﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilPublic.ServerLoger;
using StackExchange.Redis;
using UtilLib;

namespace UtilPublic.Redis
{
    /// <summary>
    /// 自增
    /// </summary>
    public partial class RedisLink
    {
        /// <summary>
        /// 自增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public long Increment<T>(T key, long value = 1)
        {
            try
            {
                return RedisDb.StringIncrement($"{key}", value);
            }
            catch
            {
                loger.Fatal(string.Format($"Increment Error {key} {value}"));
                return Util.GUID();
            }
        }
        /// <summary>
        /// 自增强制设置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void IncrementSet<T>(T key, long value = 0)
        {
            try
            {
                RedisDb.StringGetSet($"{key}", value);
            }
            catch
            {
                loger.Fatal(string.Format($"IncrementSet Error {key}"));
            }
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public long IncrementGet<T>(T key)
        {
            try
            {
                return (long)RedisDb.StringGet($"{key}");
            }
            catch
            {
                loger.Fatal(string.Format($"IdsIncrementGet Error {key}"));
                return 0;
            }
        }
        #region ID自增
        /// <summary>
        /// ID自增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public long IdsIncrement<T>(T key, int value = 1)
        {
            try
            {
                return RedisDb.StringIncrement($"Ids:{key}", value);
            }
            catch
            {
                loger.Fatal(string.Format($"IdsIncrement Error {key}"));
                return Util.GUID();
            }
        }
        /// <summary>
        /// ID自增 设置值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void IdsIncrementSet<T>(T key, long value = 0)
        {
            try
            {
                RedisDb.StringGetSet($"Ids:{key}", value);
            }
            catch
            {
                loger.Fatal(string.Format($"IdsIncrementSet Error {key}"));
            }
        }
        /// <summary>
        /// ID自增 获取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public long IdsIncrementGet<T>(T key)
        {
            try
            {
                return (long)RedisDb.StringGet($"Ids:{key}");
            }
            catch
            {
                loger.Fatal(string.Format($"IdsIncrementGet Error {key}"));
                return 0;
            }
        }
        #endregion
    }
}